<?php

Raise::load('core.helper.convert.RaiseAbstractConverter');
Raise::load('core.geometry.RaisePoint');

/**
 * RaisePoint Converter helper class
 * for conversion to RaisePoint
 * 
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.helper.convert.datatype
 * @since 1.2
 */
class RaisePointConverter extends RaiseAbstractConverter {
    
    /**
     * Convert the subject to RaisePoint
     * @param mixed $subject The subject to convert
     * @return RaisePoint The result
     */
    public function convert($subject) {
        if(RaiseVariable::isType($subject, 'RaiseRectangle')){
            return $subject->location();
        }elseif(RaiseVariable::isType($subject, array('RaiseCollection', RaiseVariable::TYPEARRAY))){
            return new RaisePoint($subject[0], $subject[1]);
        }elseif(RaiseVariable::isType($subject, RaiseVariable::TYPESTRING)){
            // parse strings like "20, 40"
            $comma = strpos($subject, ',');
            $x = RaiseConvert::to(RaiseConvert::TO_DOUBLE, trim(substr($subject, 0, $comma)));
            $y = Raiseconvert::to(RaiseConvert::TO_DOUBLE, trim(substr($subject, $comma + 1)));
            return new RaisePoint($x, $y);
        }elseif(RaiseVariable::isType($subject, 'RaisePoint')){
            return $subject;
        }
        return RaisePoint::zero();
    }
    
}